package Wires.diagram.edit.parts;

import java.util.ListIterator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.BorderItemSelectionEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ConstrainedToolbarLayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DragDropEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;

import Wires.InputActualParameter;
import Wires.InputFormalParameter;
import Wires.Model2TextTransformation;
import Wires.OutputActualParameter;
import Wires.OutputFormalParameter;
import Wires.Transformation;
import Wires.TransformationType;
import Wires.WiresFactory;
import Wires.WiresPackage;

/**
 * @generated
 */
public class Model2TextTransformationEditPart extends
		AbstractBorderedShapeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 1005;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	/**
	 * @generated
	 */
	public Model2TextTransformationEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		installEditPolicy(EditPolicyRoles.CREATION_ROLE,
				new CreationEditPolicy());
		super.createDefaultEditPolicies();
		installEditPolicy(
				EditPolicyRoles.SEMANTIC_ROLE,
				new Wires.diagram.edit.policies.Model2TextTransformationItemSemanticEditPolicy());
		installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE,
				new DragDropEditPolicy());
		installEditPolicy(
				EditPolicyRoles.CANONICAL_ROLE,
				new Wires.diagram.edit.policies.Model2TextTransformationCanonicalEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {

		ConstrainedToolbarLayoutEditPolicy lep = new ConstrainedToolbarLayoutEditPolicy() {

			protected EditPolicy createChildEditPolicy(EditPart child) {
				if (child instanceof IBorderItemEditPart) {
					return new BorderItemSelectionEditPolicy();
				}
				if (child.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE) == null) {
					if (child instanceof ITextAwareEditPart) {
						return new Wires.diagram.edit.policies.WiresTextSelectionEditPolicy();
					}
				}
				return super.createChildEditPolicy(child);
			}
		};
		return lep;
	}

	/**
	 * @generated
	 */
	protected IFigure createNodeShape() {
		Model2TextTransformationFigure figure = new Model2TextTransformationFigure();
		return primaryShape = figure;
	}

	/**
	 * @generated
	 */
	public Model2TextTransformationFigure getPrimaryShape() {
		return (Model2TextTransformationFigure) primaryShape;
	}

	/**
	 * @generated
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof Wires.diagram.edit.parts.Model2TextTransformationNameEditPart) {
			((Wires.diagram.edit.parts.Model2TextTransformationNameEditPart) childEditPart)
					.setLabel(getPrimaryShape()
							.getFigureModel2TextTransformationNameFigure());
			return true;
		}
		if (childEditPart instanceof Wires.diagram.edit.parts.WrappingLabel7EditPart) {
			((Wires.diagram.edit.parts.WrappingLabel7EditPart) childEditPart)
					.setLabel(getPrimaryShape()
							.getFigureModel2TextTransformationSepFigure());
			return true;
		}
		if (childEditPart instanceof Wires.diagram.edit.parts.WrappingLabel8EditPart) {
			((Wires.diagram.edit.parts.WrappingLabel8EditPart) childEditPart)
					.setLabel(getPrimaryShape()
							.getFigureModel2TextTransformationNameOfTypeFigure());
			return true;
		}
		if (childEditPart instanceof Wires.diagram.edit.parts.InputActualParameterEditPart) {
			BorderItemLocator locator = new BorderItemLocator(getMainFigure(),
					PositionConstants.NORTH);
			getBorderedFigure()
					.getBorderItemContainer()
					.add(
							((Wires.diagram.edit.parts.InputActualParameterEditPart) childEditPart)
									.getFigure(), locator);
			return true;
		}
		if (childEditPart instanceof Wires.diagram.edit.parts.OutputActualParameterEditPart) {
			BorderItemLocator locator = new BorderItemLocator(getMainFigure(),
					PositionConstants.SOUTH);
			getBorderedFigure()
					.getBorderItemContainer()
					.add(
							((Wires.diagram.edit.parts.OutputActualParameterEditPart) childEditPart)
									.getFigure(), locator);
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {

		if (childEditPart instanceof Wires.diagram.edit.parts.InputActualParameterEditPart) {
			getBorderedFigure()
					.getBorderItemContainer()
					.remove(
							((Wires.diagram.edit.parts.InputActualParameterEditPart) childEditPart)
									.getFigure());
			return true;
		}
		if (childEditPart instanceof Wires.diagram.edit.parts.OutputActualParameterEditPart) {
			getBorderedFigure()
					.getBorderItemContainer()
					.remove(
							((Wires.diagram.edit.parts.OutputActualParameterEditPart) childEditPart)
									.getFigure());
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, -1);
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * @generated
	 */
	protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {

		if (editPart instanceof Wires.diagram.edit.parts.InputActualParameterEditPart) {
			return getBorderedFigure().getBorderItemContainer();
		}
		if (editPart instanceof Wires.diagram.edit.parts.OutputActualParameterEditPart) {
			return getBorderedFigure().getBorderItemContainer();
		}
		return super.getContentPaneFor(editPart);
	}

	/**
	 * @generated
	 */
	protected NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(getMapMode()
				.DPtoLP(40), getMapMode().DPtoLP(40));
		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected NodeFigure createMainFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);
		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		if (nodeShape.getLayoutManager() == null) {
			ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
			layout.setSpacing(getMapMode().DPtoLP(5));
			nodeShape.setLayoutManager(layout);
		}
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated
	 */
	public EditPart getPrimaryChildEditPart() {
		return getChildBySemanticHint(Wires.diagram.part.WiresVisualIDRegistry
				.getType(Wires.diagram.edit.parts.Model2TextTransformationNameEditPart.VISUAL_ID));
	}

	/**
	 * @generated NOT
	 */
	//@Override
	protected void handleNotificationEvent(Notification notification) {
		if (notification.getEventType() == Notification.SET
				&& notification.getFeature() == WiresPackage.Literals.TYPED_ELEMENT__TYPE) {
			this.getPrimaryShape().decorar();
			Transformation transf = ((Transformation) ((View) this.getModel())
					.getElement());
			TransformationType type = (TransformationType) transf.getType();
			//Borramos todos sus parametros
			transf.getInParams().clear();
			transf.getOutParams().clear();
			//Creamos los parametros de entrada
			ListIterator<InputFormalParameter> it = type.getInParams()
					.listIterator();
			InputFormalParameter inFormalParam;
			while (it.hasNext()) {
				inFormalParam = it.next();
				InputActualParameter param = WiresFactory.eINSTANCE
						.createInputActualParameter();

				param.setName(inFormalParam.getName() + ":"
						+ inFormalParam.getTypeName());

				param.setType(inFormalParam);
				transf.getInParams().add(param);
			}
			//Creamos los parametros de salida
			ListIterator<OutputFormalParameter> it2 = type.getOutParams()
					.listIterator();
			OutputFormalParameter outFormalParam;
			while (it2.hasNext()) {
				outFormalParam = it2.next();
				OutputActualParameter param = WiresFactory.eINSTANCE
						.createOutputActualParameter();

				param.setName(outFormalParam.getName() + ":"
						+ outFormalParam.getTypeName());

				param.setType(outFormalParam);
				transf.getOutParams().add(param);
			}

		}
		super.handleNotificationEvent(notification);
	}

	/**
	 * @generated
	 */
	public class Model2TextTransformationFigure extends RoundedRectangle {

		/**
		 * @generated
		 */
		private WrappingLabel fFigureModel2TextTransformationNameFigure;
		/**
		 * @generated
		 */
		private WrappingLabel fFigureModel2TextTransformationSepFigure;
		/**
		 * @generated
		 */
		private WrappingLabel fFigureModel2TextTransformationNameOfTypeFigure;

		/**
		 * @generated
		 */
		public Model2TextTransformationFigure() {

			ToolbarLayout layoutThis = new ToolbarLayout();
			layoutThis.setStretchMinorAxis(false);
			layoutThis.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);

			layoutThis.setSpacing(5);
			layoutThis.setVertical(false);

			this.setLayoutManager(layoutThis);

			this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(30),
					getMapMode().DPtoLP(30)));
			this.setBackgroundColor(THIS_BACK);

			this.setBorder(new MarginBorder(getMapMode().DPtoLP(5),
					getMapMode().DPtoLP(5), getMapMode().DPtoLP(5),
					getMapMode().DPtoLP(5)));
			createContents();
		}

		/**
		 * @generated NOT
		 */
		private void createContents() {

			fFigureModel2TextTransformationNameFigure = new WrappingLabel();
			fFigureModel2TextTransformationNameFigure.setText("<name>");

			this.add(fFigureModel2TextTransformationNameFigure);

			fFigureModel2TextTransformationSepFigure = new WrappingLabel();
			fFigureModel2TextTransformationSepFigure.setText(":");

			this.add(fFigureModel2TextTransformationSepFigure);

			fFigureModel2TextTransformationNameOfTypeFigure = new WrappingLabel();
			fFigureModel2TextTransformationNameOfTypeFigure.setText("");

			this.add(fFigureModel2TextTransformationNameOfTypeFigure);

		}

		/**
		 * @generated NOT
		 */
		public void decorar() {
			Model2TextTransformation query = ((Model2TextTransformation) ((View) Model2TextTransformationEditPart.this
					.getModel()).getElement());
			String nameOfType = "";
			if ((query.getType() != null)
					&& (query.getType().getName() != null)) {
				nameOfType = query.getType().getName();
			}
			this.getFigureModel2TextTransformationNameOfTypeFigure().setText(
					nameOfType);
		}

		/**
		 * @generated
		 */
		private boolean myUseLocalCoordinates = false;

		/**
		 * @generated
		 */
		protected boolean useLocalCoordinates() {
			return myUseLocalCoordinates;
		}

		/**
		 * @generated
		 */
		protected void setUseLocalCoordinates(boolean useLocalCoordinates) {
			myUseLocalCoordinates = useLocalCoordinates;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureModel2TextTransformationNameFigure() {
			return fFigureModel2TextTransformationNameFigure;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureModel2TextTransformationSepFigure() {
			return fFigureModel2TextTransformationSepFigure;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureModel2TextTransformationNameOfTypeFigure() {
			return fFigureModel2TextTransformationNameOfTypeFigure;
		}

	}

	/**
	 * @generated
	 */
	static final Color THIS_BACK = new Color(null, 254, 255, 203);

}
